package com.ruoyi.project.roadnet.baseRoadwaysection.controller;

import java.util.List;

import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataScope;
import com.ruoyi.project.roadnet.baseRoadwaysection.domain.BaseRoadway;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.roadnet.baseRoadwaysection.domain.BaseRoadwaysection;
import com.ruoyi.project.roadnet.baseRoadwaysection.service.IBaseRoadwaysectionService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 路段信息操作处理
 * 
 * @author zhangdachen
 * @date 2019-04-12
 */
@Controller
@RequestMapping("/roadnet/baseRoadwaysection")
public class BaseRoadwaysectionController extends BaseController
{
    private String prefix = "roadnet/baseRoadwaysection";
	
	@Autowired
	private IBaseRoadwaysectionService baseRoadwaysectionService;
	
	@RequiresPermissions("roadnet:baseRoadwaysection:view")
	@GetMapping("/roadway")
	public String baseRoadway()
	{
	    return prefix + "/baseRoadway";
	}

	@RequiresPermissions("roadnet:baseRoadwaysection:view")
	@GetMapping("/roadwaySection/{roadcode}")
	public String baseRoadwaySection(@PathVariable("roadcode") String roadcode, ModelMap mmap)
	{
		mmap.addAttribute("roadcode", roadcode);
		return prefix + "/baseRoadwaysection";
	}
	/**
	 * 查询路线列表
	 */
	@RequiresPermissions("roadnet:baseRoadwaysection:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(BaseRoadway baseRoadway)
	{
		startPage();
		List<BaseRoadway> list =baseRoadwaysectionService.selectBaseRoadwayList(baseRoadway);
		return getDataTable(list);

	}
	/**
	 * 查询路段列表
	 */
	@RequiresPermissions("roadnet:baseRoadwaysection:list")
	@PostMapping("/Roadwaysectionlist/{roadcode}")
	@ResponseBody
	public TableDataInfo Roadwaysectionlist(@PathVariable("roadcode") String roadcode)
	{
		startPage();
		List<BaseRoadwaysection> list =baseRoadwaysectionService.selectBaseRoadwaysectionListByRoadCode(roadcode);
		return getDataTable(list);
	}
	
	/**
	 * 导出路段列表
	 */
	@RequiresPermissions("roadnet:baseRoadwaysection:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BaseRoadwaysection baseRoadwaysection)
    {
    	List<BaseRoadwaysection> list = baseRoadwaysectionService.selectBaseRoadwaysectionList(baseRoadwaysection);
        ExcelUtil<BaseRoadwaysection> util = new ExcelUtil<BaseRoadwaysection>(BaseRoadwaysection.class);
        return util.exportExcel(list, "baseRoadwaysection");
    }


	/**
	 * 道路地图视图
	 */
	@GetMapping("/roadwaymap/{id}")
	public String map(@PathVariable("id") String id, ModelMap mmap)
	{
		List<BaseRoadwaysection> list = baseRoadwaysectionService.selectBaseRoadwaysectionListByRoadCode(id);
		for (BaseRoadwaysection item:list ) {
			String shape = item.getShape();
			Integer start = shape.indexOf('(');
			Integer end = shape.indexOf(')');
			String[] shapeOld = shape.substring(start + 1, end - 1 - start).split(",");
			Integer i=0;
			Integer length = shapeOld.length;
			String[] result = shapeOld[length / 2].split(" ");
			item.setX(Double.valueOf(result[2]));
			item.setY(Double.valueOf(result[1]));
		}
		mmap.put("list", list);
		return prefix + "/map";
	}



	/**
	 * 新增路段
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存路段
	 */
	@RequiresPermissions("roadnet:baseRoadwaysection:add")
	@Log(title = "路段", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(BaseRoadwaysection baseRoadwaysection)
	{		
		return toAjax(baseRoadwaysectionService.insertBaseRoadwaysection(baseRoadwaysection));
	}

	/**
	 * 修改路段
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		BaseRoadwaysection baseRoadwaysection = baseRoadwaysectionService.selectBaseRoadwaysectionById(id);
		mmap.put("baseRoadwaysection", baseRoadwaysection);
	    return prefix + "/edit";
	}
	/**
	 * 查看路段
	 */
	@GetMapping("/view/{id}")
	public String view(@PathVariable("id") Integer id, ModelMap mmap)
	{
		BaseRoadwaysection baseRoadwaysection = baseRoadwaysectionService.selectBaseRoadwaysectionById(id);
		mmap.put("baseRoadwaysection", baseRoadwaysection);
		return prefix + "/view";
	}

	
	/**
	 * 修改保存路段
	 */
	@RequiresPermissions("roadnet:baseRoadwaysection:edit")
	@Log(title = "路段", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(BaseRoadwaysection baseRoadwaysection)
	{		
		return toAjax(baseRoadwaysectionService.updateBaseRoadwaysection(baseRoadwaysection));
	}
	
	/**
	 * 删除路段
	 */
	@RequiresPermissions("roadnet:baseRoadwaysection:remove")
	@Log(title = "路段", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(baseRoadwaysectionService.deleteBaseRoadwaysectionByIds(ids));
	}
	
}
